<?php
/*reated by PhpStorm.
 * Author: 赵凯
 * Date: 2019/12/10
 * Time: 16:08
 */

namespace api\install\controller;


use api\common\model\RlApplyDelayModel;
use api\common\model\RlBmNodeRelModel;
use api\common\model\RlCompanyModel;
use api\common\model\RlNodeModel;
use api\common\model\RlTypeModel;
use api\common\model\RlEtAzdModel;
use api\common\model\RlEtAzsModel;
use api\common\model\RlEtCompanyModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlHiseServiceProjectModel;
use api\common\model\RlZsmsUserQueryModel;
use api\common\service\CurlService;
use Knp\Snappy\Pdf;
use Mpdf\Mpdf;
use think\Db;
use api\common\service\FunctionService;
use think\exception\DbException;
use api\common\service\MsgService;
use api\common\yueke\YuekeController;
use think\Cache;
class InstallController extends BaseController
{
    protected $status = [
        '未进场' => 0,
        '已进场' => 1,
        '隐蔽施工中' => 2,
        '隐蔽施工已结束' => 3,
        '调试' => 4,
        '运行' => 5,
        '作废' => 'X',

    ];
    protected $install_status_new = [
        0 => '未进场',
        1 => '已进场',
        2 => '隐蔽施工中',
        3 => '隐蔽施工已结束',
        4 => '调试',
        5 => '运行',
        'X' => '作废',
    ];
    protected $dispatch = [

        '待安装商派工' => 1,//安装商
        '待安装队接受' => 2,//安装商
        '安装队已接受' => 3,//安装队
        '安装商已退回' => 4,
        '安装队已退回' => 5,


    ];

    //已完成
    public function alreadyInstalled()
    {
        $install_status = $this->status['运行'];
        // $jxs = $this->request->param('jxs');
        // if(!$jxs) $this->error('参数错误');
        $where['gczt'] = $install_status;
        $where['zzdpdzt'] = $this->dispatch['安装队已接受'];
        // $where['jxs'] = $jxs;
        $where['azd'] = $this->user_info['engineer_code'];
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $where = array_merge($where, $selectazd);
        $field = 'azd.engineer_name,hise.yh_name,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.kqshzt,hise.sfkaz_bs,hise.sfkq_bs,hise.jxsxsy,hise.gczt,hise.hise_service_project_id';
        $pege = input('param.pege');
        $limit = input('param.limit');
        $start = ($pege - 1) * $limit;
        $list = RlHiseServiceProjectModel::getHiseList($where, $field, $start, $limit);
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['problem_num'] = Db::name('rl_inspect')->where('azd_id', $val['azd_id'])->where('is_problem', 1)->where('inspect_status', 2)->count();
            $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
        }
        $data['user_info'] = $this->user_info;
        $data['data'] = $list;
        $this->success('请求成功', '', $data);
    }

    //正在进行
    public function waitInstalled()
    {

        // $jxs = $this->request->param('jxs','');
        // if(!$jxs) $this->error('参数错误');
        $where['gczt'] = ['in', $this->status['未进场'] . ',' . $this->status['已进场'] . ',' . $this->status['隐蔽施工中'] . ',' . $this->status['隐蔽施工已结束'] . ',' . $this->status['调试']];
        $where['zzdpdzt'] = $this->dispatch['安装队已接受'];
        $where['azd'] = $this->user_info['engineer_code'];
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $where = array_merge($where, $selectazd);
        $field = 'azd.engineer_name,hise.yh_name,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.ycgcwcsj as yyaz_sj,hise.jxsxsy,hise.gczt,hise.hise_service_project_id,hise.kqshzt,hise.sfkaz_bs,hise.sfkq_bs';
        $pege = input('param.pege');
        $limit = input('param.limit');
        $start = ($pege - 1) * $limit;
	//dump($where);die;
        $list = RlHiseServiceProjectModel::getHiseList($where, $field, $start, $limit);
        //$list = RlHiseServiceProjectModel::getHiseList($where,$field);
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['problem_num'] = Db::name('rl_inspect')->where('azd_id', $val['azd_id'])->where('is_problem', 1)->where('inspect_status', 2)->count();
            $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
        }
        $data['user_info'] = $this->user_info;
        $data['data'] = $list;

        $this->success('请求成功', '', $data);
    }

    //待接收
    public function waitReceived()
    {
        $dispatch_status = $this->dispatch['待安装队接受'];
        $where['zzdpdzt'] = $dispatch_status;
        $where['gczt'] = ['<>', 'X'];
        $where['azd'] = $this->user_info['engineer_code'];
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $where = array_merge($where, $selectazd);
        $field = 'azd.engineer_name,hise.yh_name,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.fgsfwfzr,hise.kqshzt,hise.sfkaz_bs,hise.sfkq_bs,hise.gczt,hise.jxsxsy,hise.hise_service_project_id,zzdpdzt';
        $pege = input('param.pege');
        $limit = input('param.limit');
        $start = ($pege - 1) * $limit;
        $list = RlHiseServiceProjectModel::getHiseList($where, $field, $start, $limit);

        //   $list = RlHiseServiceProjectModel::getHiseList($where,$field);
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['problem_num'] = Db::name('rl_inspect')->where('azd_id', $val['azd_id'])->where('is_problem', 1)->where('inspect_status', 2)->count();
            $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
        }
        $data['user_info'] = $this->user_info;
        $data['data'] = $list;
        $this->success('请求成功', '', $data);
    }

    //接受/拒绝派工单
    public function acceptInstall()
    {
        $service_project_id = $this->request->param('service_project_id/a', []);
        $dispatch_status = $this->request->param('dispatch_status', 3);
        $zzdpdzt = RlHiseServiceProjectModel::Where('hise_service_project_id', 'in', $service_project_id)->select()->toArray();
        foreach ($zzdpdzt as $key => $value) {
            if ($value['zzdpdzt'] != 2) {
                $this->success('不能进行此操作');
            }
        }
        $status = 0;
        if ($dispatch_status == 4) {
            $status = $this->status['未进场'];
        }
        if (empty($service_project_id)) $this->error('ID不能为空');

        $data['gczt'] = $status;
        $data['zzdpdzt'] = $dispatch_status;
        $data['update_time'] = time();
        $res = RlHiseServiceProjectModel::Where('hise_service_project_id', 'in', $service_project_id)->update($data);

        $msg = '拒绝';
        if ($dispatch_status == 3) $msg = '接受';
        if ($res) {
            if (config('installsend2') && $dispatch_status != 3) {
                // 引用发送验证码类
                $msg_send = new MsgService();
                //获取安装队手机号信息
                foreach ($zzdpdzt as $v) {
                    $azd_id = $v['azd_id'];
                    $phone = $v['xsy_sj'];
                    // 发送手机验证码
                    $msgres = $msg_send->installsend2($azd_id, $phone);
                    if ($msgres) {
                        trace($phone . '||' . date('Y-m-d h:i:s', time()) . 'installsend2短信发送成功', 'info');
                    } else {
                        trace($phone . '||' . date('Y-m-d h:i:s', time()) . 'installsend2短信发送失败', 'info');
                    }
                }
            }
            $this->success($msg . '安装单成功');
        }
        $this->error($msg . '安装单失败');

    }

    //工程信息
    public function installInfo()
    {
        $service_project_id = $this->request->param('service_project_id', '');
        if (!$service_project_id) $this->error('参数错误');
        //多表引入查询方法
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->field('yh_name,xb,sj_one,provice,city,qx,address,qyfgs,jxs
        ,azs,azd,fgsfwfzr,cplb,yyjc_sj,jkh,zjbxq,zybjbxq,jxsxsy,bz,pic_one,pic_two,yyaz_sj,xsy_sj,gczt')->find();
        if (!$info) $this->error('该用户不存在');
        if (empty($info['qx'])) {
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code', $info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice . $city . $info['address'];
            $info['address_cp'] = $info['address'];
            $info['address'] = $provice . $city;
        } else {
            $xq = Db::name('rl_hise_qx_table')->where('qx_code', $info['qx'])->field('qx_name')->find()['qx_name'];
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code', $info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice . $city . $xq . $info['address'];
            $info['address_cp'] = $info['address'];
            $info['address'] = $provice . $city . $xq;
        }
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm', $info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $info['azs_name'] = RlEtAzsModel::where('server_code', $info['azs'])->field('server_name')->find()['server_name'];
        $info['azd_name'] = RlEtAzdModel::where('engineer_code', $info['azd'])->field('engineer_name')->find()['engineer_name'];
        $info['azd_phone'] = RlEtAzdModel::where('engineer_code', $info['azd'])->field('phone')->find()['phone'];
        $info['fgs_name'] = Db::name('rl_company')->where('orgdept', $info['qyfgs'])->field('orgname')->find()['orgname'];

        $cplb_arr = explode('/', $info['cplb']);
        $cplb_str = implode(',', $cplb_arr);
        $product_name_arr = Db::name('rl_et_product_xcx')->where('product_id', 'in', $cplb_str)->column('product_name');
        $info['cplb'] = implode('/', $product_name_arr);

        if ($info['yyjc_sj']) {
            $info['yyjc_sj'] = date('Y-m-d', $info['yyjc_sj']);
        }
        if ($info['yyaz_sj']) {
            $info['yyaz_sj'] = date('Y-m-d', $info['yyaz_sj']);
        }
        $data['info'] = $info;
        $this->success('请求成功', '', $data);


    }

    //预约时间
    public function approach()
    {
        $service_project_id = $this->request->param('service_project_id', '');
        if (!$service_project_id) $this->error('参数错误');
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->field('yyjc_sj,sjjc_sj,gps_address')->find()->toArray();

        if (!$info['yyjc_sj']) {
            $data['status'] = 0;
        } else {
            if ($info['sjjc_sj']) {
                $data['status'] = 2;
            } else {
                $data['status'] = 1;
            }
        }
        $data = array_merge($data, $info);
        $data['yyjc_sj'] = date('Y-m-d H:i', $data['yyjc_sj']);
        $data['sjjc_sj'] = date('Y-m-d H:i', $data['sjjc_sj']);
        $this->success('请求成功', '', $data);
    }

    //更改预约时间
    public function updateApproach()
    {
        $service_project_id = $this->request->param('service_project_id', '');
        $time = $this->request->param('yyjc_sj', '');
        if (!$service_project_id) $this->error('参数错误');
        $data['yyjc_sj'] = strtotime($time);
        $data['update_time'] = time();
//dump($data);die;
        $res = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->update($data);
        if ($res) {
            if ($data['yyjc_sj'] && config('installsend10')) {
                // 引用发送验证码类
                $msg_send = new MsgService();
                // 发送手机验证码
                $datas = $this->PrepositionReport($service_project_id);
                $address_detail = $datas['info']['address_detail'];
                $yyaz_sj = date('Y-m-d', $data['yyjc_sj']);
                $h = date('H', $data['yyjc_sj']);
                if ($h < 12) $yyaz_sj = $yyaz_sj . "上午";
                else if ($h < 19) $yyaz_sj = $yyaz_sj . "下午";
                $phone = $datas['info']['sj_one'];
                $msgres = $msg_send->installsend10($address_detail, $phone, $yyaz_sj);
                if ($msgres) {
                    trace($phone . '||' . date('Y-m-d h:i:s', time()) . '预约短信发送成功', 'info');
                } else {
                    trace($phone . '||' . date('Y-m-d h:i:s', time()) . '预约短信发送失败', 'info');
                }
            }
            $this->success('设置预约安装时间成功');
        }
        $this->error('设置预约安装时间失败');
    }

    //更新进场时间
    public function updateFactApproach()
    {
        $sjjc_sj = $this->request->param('sjjc_sj', '');
        $gps_address = $this->request->param('gps_address', '');
        $service_project_id = $this->request->param('service_project_id', '');
        if (!$sjjc_sj || !$gps_address || !$service_project_id) $this->error('参数错误');
        $data = ['sjjc_sj' => strtotime($sjjc_sj),
            'gps_address' => $gps_address,
            'gczt' => $this->status['已进场']
        ];
        $data['update_time'] = time();
        $res = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->update($data);
        if ($res) {
            if (config('installsend3')) {
                // 引用发送验证码类
                $msg_send = new MsgService();
                //获取安装队手机号信息
                $item = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->find();
                $azd = RlEtAzdModel::Where('engineer_code', $item['azd'])->find();
                $azd_id = $item['azd_id'];
                $address = $item['azd_name'];
                $jxs_name = RlEtCustomerModel::where('jxsbm', $item['jxs'])->field('jxs_mc')->find()['jxs_mc'];
                $phoneNumber = $azd['phone'];
                $azd_name = $azd['engineer_name'];
                //经销商权限的账号
                if (config('installsend31')) {
                    $jxs = RlZsmsUserQueryModel::Where('bm_code', $item['jxs'])->where('gw', 0)->where('status', 1)->select()->toArray();
                    foreach ($jxs as $key => $value) {
                        $msgres = $msg_send->installsend3($azd_id, $value['mobilphone'], $azd_name, $phoneNumber, $jxs_name, $address);
                        if ($msgres) {
                            trace($value['mobilphone'] . '||' . date('Y-m-d h:i:s', time()) . 'installsend31短信发送成功', 'info');
                        } else {
                            trace($value['mobilphone'] . '||' . date('Y-m-d h:i:s', time()) . 'installsend31短信发送失败', 'info');
                        }
                    }
                }
                //分公司服务工程师
                if (config('installsend32')) {
                    $jxs = RlZsmsUserQueryModel::Where('bm_code', $item['fwfgs'])->where('user_name', $item['fgsfwfzr'])->where('gw', 5)->where('status', 1)->select()->toArray();
                    foreach ($jxs as $key => $value) {
                        $msgres = $msg_send->installsend3($azd_id, $value['mobilphone'], $azd_name, $phoneNumber, $jxs_name, $address);
                        if ($msgres) {
                            trace($value['mobilphone'] . '||' . date('Y-m-d h:i:s', time()) . 'installsend32短信发送成功', 'info');
                        } else {
                            trace($value['mobilphone'] . '||' . date('Y-m-d h:i:s', time()) . 'installsend32短信发送失败', 'info');
                        }
                    }
                }
                //分公司服务经理
                if (config('installsend33')) {
                    $jxs = RlZsmsUserQueryModel::Where('bm_code', $item['fwfgs'])->where('user_name', $item['fgsfwfzr'])->where('gw', 3)->where('status', 1)->select()->toArray();
                    foreach ($jxs as $key => $value) {
                        $msgres = $msg_send->installsend3($azd_id, $value['mobilphone'], $azd_name, $phoneNumber, $jxs_name, $address);
                        if ($msgres) {
                            trace($value['mobilphone'] . '||' . date('Y-m-d h:i:s', time()) . 'installsend33短信发送成功', 'info');
                        } else {
                            trace($value['mobilphone'] . '||' . date('Y-m-d h:i:s', time()) . 'installsend33短信发送失败', 'info');
                        }
                    }
                }

            }
            $azd_id = $res['azd_id'];
            $this->updataInspect($azd_id, $data['gczt']);
            $this->success('设置成功');
        }
        $this->error('设置失败');
    }

    //筛选节点
    public function screenNode()
    {
        $type_arr = $this->request->param('type_id/a', []);
        $service_project_id = $this->request->param('service_project_id', '');
        if (!$service_project_id || empty($type_arr)) $this->error('参数错误');
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->field('gczt,type,create_time,jxs,azd_id,node_ids')->find();
        if ($info['gczt'] == $this->status['隐蔽施工已结束'] || $info['gczt'] == $this->status['调试']) $this->error('隐蔽施工已结束不能再更改节点类型');
        $type_str = implode(',', $type_arr);
        $data['type'] = $type_str;
        $product_type = Db::name('rl_type')->where('type_id', 'in', $type_str)->column('product_type');
        $product_type = implode(',', $product_type);
        $data['product_type'] = $product_type;
        $data['update_time'] = time();
        //判断分类是新增还是减少，如果是减少就删除对应的节点照片，条码，巡检
        if ($info['type']) {
            $out_arr = explode(',', $info['type']);
        } else {
            $out_arr = [];
        }
        foreach ($out_arr as $key => $value) {
            if (in_array($value, $type_arr)) {
                unset($out_arr[$key]);
            }
        }

        $hang_type_id = Db::name('rl_type')->where('is_hang', 1)->value('type_id');
        //判断是否修改柜挂机分类，如果是，返回信息提示不可修改
        if ((in_array($hang_type_id, $type_arr) && in_array($hang_type_id, $out_arr)) || (!in_array($hang_type_id, $type_arr) && !in_array($hang_type_id, $out_arr))) {

        } else {
            $this->error('柜挂机分类不可以修改，不可以和其他分类一起选择');
        }
        if ($out_arr) {
            //如果有删除
            // 启动事务
            Db::startTrans();
            try {
                //判断时间，删除数据
                if ($info['create_time'] > strtotime('2020-05-09')) {
                    foreach ($out_arr as $keys => $values) {
                        //查询对应的节点数组
                        /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/

                        // $company = RlEtCompanyModel::Where('jxsbm', $info['jxs'])->field('fgs')->find();
                        // $where = $info['jxs'].','.$company['fgs'].'.'.$info['jxs'].','.'1';
                        // $list = RlBmNodeRelModel::where('bm','in',$where)->field('node_id')->where('type_id',$values)->select()->toArray();
                        // $str = '';

                        // foreach ($list as $k =>$v){
                        //     if($k==0){
                        //         $str = $str.$v['node_id'];
                        //     }else{
                        //         $str = $str.','.$v['node_id'];
                        //     }
                        // }
                        $str = $info['node_ids'];
                        /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/

                        //根据安装单id和节点id删除节点数据和条码数据
                        Db::name('rl_hise_service_pro_process')->where('gc_azdid', $info['azd_id'])->where('azjd', 'in', $str)->delete();
                        $zpdz = Db::name('rl_hise_service_pro_inst_detail')->where('azd_id', $info['azd_id'])->where('node_id', 'in', $str)->column('zpdz');
                        foreach ($zpdz as $key => $value) {
                            if ($value) {
                                unlink(ROOT_PATH . 'public' . $value);
                            }
                        }
                        $process = Db::name('rl_hise_service_pro_process')->where('gc_azdid', $info['azd_id'])->where('azjd', 'in', $str)->select()->toArray();
                        foreach ($process as $key => $value) {
                            if ($value['tp_one']) {
                                unlink(ROOT_PATH . 'public' . $value['tp_one']);
                            }
                            if ($value['tp_two']) {
                                unlink(ROOT_PATH . 'public' . $value['tp_two']);
                            }
                            if ($value['tp_there']) {
                                unlink(ROOT_PATH . 'public' . $value['tp_there']);
                            }
                        }
                        Db::name('rl_hise_service_pro_inst_detail')->where('azd_id', $info['azd_id'])->where('node_id', 'in', $str)->delete();
                        //根据安装单查询巡检单编号
                        $inspect_code = Db::name('rl_inspect')->where('azd_id', $info['azd_id'])->column('inspect_code');
                        //根据巡检单编号和节点西信息查询需要删除的巡检单
                        $del_inspect_code = Db::name('rl_inspect_step')->where('inspect_code', 'in', $inspect_code)->where('node', 'in', $str)->column('inspect_code');
                        //删除巡检相关数据
                        $step = Db::name('rl_inspect_step')->where('inspect_code', 'in', $del_inspect_code)->select()->toArray();
                        foreach ($step as $key => $value) {
                            if ($value['specs_pic']) {
                                unlink(ROOT_PATH . 'public' . $value['specs_pic']);
                            }
                            if ($value['specs_pic_two']) {
                                unlink(ROOT_PATH . 'public' . $value['specs_pic_two']);
                            }
                            if ($value['specs_pic_three']) {
                                unlink(ROOT_PATH . 'public' . $value['specs_pic_three']);
                            }
                        }
                        $step_checking = Db::name('rl_inspect_step_checking')->where('inspect_code', 'in', $del_inspect_code)->select()->toArray();
                        foreach ($step_checking as $key => $value) {
                            if ($value['pic_one']) {
                                unlink(ROOT_PATH . 'public' . $value['pic_one']);
                            }
                            if ($value['pic_two']) {
                                unlink(ROOT_PATH . 'public' . $value['pic_two']);
                            }
                            if ($value['pic_three']) {
                                unlink(ROOT_PATH . 'public' . $value['pic_three']);
                            }
                            if ($value['pic_four']) {
                                unlink(ROOT_PATH . 'public' . $value['pic_four']);
                            }
                            if ($value['pic_five']) {
                                unlink(ROOT_PATH . 'public' . $value['pic_five']);
                            }
                        }
                        Db::name('rl_inspect')->where('inspect_code', 'in', $del_inspect_code)->delete();
                        Db::name('rl_inspect_step')->where('inspect_code', 'in', $del_inspect_code)->delete();
                        Db::name('rl_inspect_score_scoring')->where('inspect_code', 'in', $del_inspect_code)->delete();
                        Db::name('rl_inspect_step_checking')->where('inspect_code', 'in', $del_inspect_code)->delete();
                    }
                }
                $res = RlHiseServiceProjectModel::where('hise_service_project_id', $service_project_id)->update($data);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $res = 0;
            }
        } else {
            //如果增加
            $res = RlHiseServiceProjectModel::where('hise_service_project_id', $service_project_id)->update($data);
        }
        if ($res) $this->success('设置节点类型成功');
        $this->error('设置节点类型失败');
    }

    //节点列表
    public function nodeList()
    {
        /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
        // $str = $this->nodePreposition();
        $azd_id_p = $this->request->param('azd_id', '');
        $type_id_p = $this->request->param('type_id', '');
        $info = RlHIseServiceProjectModel::Where('azd_id', $azd_id_p)->find();
        $str = $info['node_ids'];
        $data['node_list'] = Db::name('rl_node')->where('type_id', $type_id_p)->where('node_id', 'in', $str)->order('sort desc')->field('node_id,type_id,node_name,is_up_txm,is_hide')->select()->toArray();
        $p = Db::name('rl_hise_service_pro_process')->where('gc_azdid', $azd_id_p)->where('cplp', $type_id_p)->column('azjd');
        /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
        // $node_data = Db::name('rl_node_data')->where('pid','in',$str)->select()->toArray();
        foreach ($data['node_list'] as $key => $value) {
            // foreach ($node_data as $ke => $va) {
            //     if($value['node_id'] == $va['pid'])
            //     {
            //         $data['node_list'][$key]['data'][] = $va;
            //     }
            // }
            if (in_array($value['node_id'], $p)) {
                $data['node_list'][$key]['is_ok'] = 1;
            } else {
                if ($info['gczt'] == 5) {
                    if ($info['zdyxbs'] != 1) {
                        unset($data['node_list'][$key]);
                    }
                } else {

                    $data['node_list'][$key]['is_ok'] = 0;
                }
            }
        }
        $this->success('请求成功', '', $data);

    }

    //展示节点类别
    public function showNode()
    {
        $service_project_id = $this->request->param('service_project_id', '');
        $where = [];
        if ($service_project_id) {
            $res = RlHiseServiceProjectModel::where('hise_service_project_id', $service_project_id)->field('type')->find();
            $type = $res['type'];
            $where['type_id'] = ['in', $type];
        }

        $row = Db::name('rl_type')->where($where)->select();
        if ($row) $this->success('请求成功', '', ['type' => $row]);
        $this->error('请求失败');

    }

    //删除上传节点图片后未保存的图片
    public function delfilearr()
    {
        $removeimage = [];
        $removeimage = $this->request->param("fileArr/a");
        foreach ($removeimage as $keyi => $valuei) {
            unlink(ROOT_PATH . 'public' . $valuei['url']);
        }
        $this->success('删除成功');
    }

    //上传节点图片
    public function uploadNode()
    {
        $type_id = $this->request->param('type_id', '');
        $node_id = $this->request->param('node_id', '');
        $azdid = $this->request->param('azd_id', '');
        $node_name = $this->request->param('node_name', '');
        $arrayFile = $this->request->param("image/a");
        if (!$type_id || !$node_id || !$azdid || empty($arrayFile) || !$node_name) $this->error('参数错误');
        $res = RlHiseServiceProjectModel::where('azd_id', $azdid)->field('gczt')->find()['gczt'];
        if (!$res) $this->error('未进场不能提交节点');
        $data_up = [];
        $node_info = Db::name('rl_node')->where('node_id', $node_id)->field('is_hide,is_up_txm,in_and_ex')->find();
        $is_hide = $node_info['is_hide'];
        $is_up_txm = $node_info['is_up_txm'];
        $in_and_ex = $node_info['in_and_ex'];
        if ($is_up_txm) {
            $txm_is_up = Db::name('rl_hise_service_pro_inst_detail')->where('azd_id', $azdid)->where('node_id', $node_id)->find();
            if ($in_and_ex == 2) {
                if (empty($txm_is_up)) $this->error('请先上传该节点条形码！');
            }
        }
        $product_type = Db::name('rl_type')->where('type_id', $type_id)->field('product_type')->find()['product_type'];
        $data['cplp'] = $type_id;
        $data['azjd'] = $node_id;
        $data['azjd_name'] = $node_name;
        $data['gc_azdid'] = $azdid;
        $data['jdqrsj'] = time();
        $data['update_time'] = time();
        $data['product_type'] = $product_type;
//dump($arrayFile);
        foreach ($arrayFile as $k => $File) {
            if (!$File) $this->error('图片为空，不能保存');
            if ($k == 0) $data['tp_one'] = $File;
            if ($k == 1) $data['tp_two'] = $File;
            if ($k == 2) $data['tp_there'] = $File;
            if ($k == 3) $data['tp_four'] = $File;
            if ($k == 4) $data['tp_five'] = $File;
        }
        $rl_hise_service_pro_process = Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->find();
        //控制可不可以进行修改图片
       // if ($rl_hise_service_pro_process) {
         //   $this->error('此节点已保存，不能重复保存');
        //}
	 if(Cache::has('end:'.$azdid)){
            $this->error("请稍后尝试");
        }else{
            Cache::set('end:'.$azdid,1,3);
        }
	$removeimages = $this->request->param("removeimage/a");
    	if(isset($removeimages)){
		foreach($removeimages as $kimage=>$vimage){
                   $tp_one=Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->value('tp_one');
                    if($tp_one==$vimage){
				Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->setField('tp_one','');
			}
			 $tp_two=Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->value('tp_two');
                    if($tp_two==$vimage){
                                Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->setField('tp_two','');
                        }
			$tp_there=Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->value('tp_there');
                    if($tp_there==$vimage){
                                Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->setField('tp_there','');
                        }
                     $tp_four=Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->value('tp_four');
                    if($tp_four==$vimage){
                                Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->setField('tp_four','');
                        }
			$tp_five=Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->value('tp_five');

			if($tp_five==$vimage){
                                Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->setField('tp_five','');
                        }






		}
	}
	if($rl_hise_service_pro_process){
		 Db::name('rl_hise_service_pro_process')->where('azjd', $node_id)->where('gc_azdid', $azdid)->update($data);
	}
        $res = Db::name('rl_hise_service_pro_process')->insert($data);
        if ($res) {
            if ($is_hide) {
                $info = RlHIseServiceProjectModel::where('azd_id', $azdid)->field('jxs,type,node_ids')->find();
                /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/

                // $company = RlEtCompanyModel::where('jxsbm', $info['jxs'])->field('fgs')->find();
                // $where = $info['jxs'].','.$company['fgs'].'.'.$info['jxs'].','.'1';
                // $list = RlBmNodeRelModel::where('bm','in',$where)->field('node_id')->where('type_id','in',$info['type'])->select();

                // $str = '';
                // foreach ($list as $k =>$v){
                //     if($k==0){
                //         $str = $str.$v['node_id'];
                //     }else{
                //         $str = $str.','.$v['node_id'];
                //     }
                // }
                $str = $info['node_ids'];
                $arr_count = Db::name('rl_node')->where('type_id', 'in', $info['type'])->where('node_id', 'in', $str)->column('node_id');
                $str = implode(',', $arr_count);
                /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
                $data_up = ['gczt' => $this->status['隐蔽施工中'], 'update_time' => time()];
                $node_list = Db::name('rl_node')->where('node_id', 'in', $str)->where('is_hide', 1)->order('sort desc')->column('node_id');
                $pro_node_list = Db::name('rl_hise_service_pro_process')->where('gc_azdid', $azdid)->column('azjd');
                if (empty(array_diff($node_list, $pro_node_list))) {
                    $data_up = ['gczt' => $this->status['隐蔽施工已结束'], 'ycgcsfjsbs' => 1, 'ycgcwcsj' => time(), 'update_time' => time()];
                    RlHIseServiceProjectModel::where('azd_id', $azdid)->update($data_up);
                    //push门店内机数据
                    $azdData=RlHIseServiceProjectModel::where('azd_id',$azdid)->find();
                    $nodeData=Db::table('cmf_rl_hise_service_pro_process')->where(['gc_azdid'=>$azdid])->select();
                    if($azdData['bbdh']!=''){
                        $dataaz['RECORD_NO']=$azdData['bbdh'];
                        $dataaz['CTIME']=date('Y-m-d H:i:s');
                        $dataaz['INDOOR_NUM']=0;
                        $dataaz['INDOOR_SAP']=[];
                        $dataaz['BRAND']=2;
                        $dataaz['SERVER_CODE']=$azdData['azs'];
                        $dataaz['SERVER_TEAM']=$azdData['azd'];
                        $dataaz['SERVER_CAPTAIN']='';
                        $dataaz['SERVER_MOBILE']='';
                        //节点照片
                        $dataaz['NODE']=[];
                        if($azdData['azd']){
                            $dataaz['SERVER_CAPTAIN']=Db::table('cmf_rl_et_azd')->where([
                                'engineer_code'=>$azdData['azd'],
                                'active_flag'=>1
                            ])->value('engineer_name');
                            $dataaz['SERVER_MOBILE']=Db::table('cmf_rl_et_azd')->where([
                                'engineer_code'=>$azdData['azd'],
                                'active_flag'=>1
                            ])->value('phone');
                        }


                        $tiaomaData=Db::table('cmf_rl_hise_service_pro_inst_detail')->where([
                            'azd_id'=>$azdid
                        ])->select();

                        foreach($tiaomaData as $kn=>$vn){
                            $xlhData=Db::table('cmf_rl_node')->where([
                                'node_id'=>$vn['node_id']
                            ])->value('in_and_ex');
                            if($xlhData==1){
                                $dataaz['INDOOR_SAP'][]=[
                                    'CPJX'=>$vn['xhdm'],
                                    'JQXLH'=>$vn['jqxlh'],
                                    'HT_ON'=>$vn['chht']
                                ];
                            }

                        }
                        $dataaz['INDOOR_NUM']=count($dataaz['INDOOR_SAP']);



                        if($nodeData){
                            foreach($nodeData as $knode=>$vnode){
                                $arr['azjd_name']=Db::table('cmf_rl_node')->where(['node_id'=>$vnode['azjd']])->value('node_name');
                                $arr['product_type']=$vnode['product_type'];
                                $arr['azjd']=$vnode['azjd'];
                                $arr['img']=[];
                                if($vnode['tp_one']){
                                    $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_one'];
                                }
                                if($vnode['tp_two']){
                                    $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_two'];
                                }
                                if($vnode['tp_there']){
                                    $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_there'];
                                }
                                if($vnode['tp_four']){
                                    $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_four'];
                                }
                                if($vnode['tp_five']){
                                    $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_five'];
                                }
                                $dataaz['NODE'][]=$arr;
                            }
                        }
                        $dataaz['NODE']=json_encode( $dataaz['NODE']);
                        $dataaz['INDOOR_SAP']=json_encode( $dataaz['INDOOR_SAP']);
                        $Yueke=new YuekeController();
                        $Yueke->PushAzdNeiData($dataaz);
                    }
                    //发送内机数据结束
                }
            }
            $info = RlHIseServiceProjectModel::Where('azd_id', $azdid)->field('jxs,type,node_ids')->find();
            /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/

            // $company = RlEtCompanyModel::Where('jxsbm', $info['jxs'])->field('fgs')->find();
            // $where = $info['jxs'].','.$company['fgs'].'.'.$info['jxs'].','.'1';
            // $list = RlBmNodeRelModel::where('bm','in',$where)->field('node_id')->where('type_id','in',$info['type'])->select()->toArray();
            // $str = '';
            // foreach ($list as $k =>$v){
            //     if($k==0){
            //         $str = $str.$v['node_id'];
            //     }else{
            //         $str = $str.','.$v['node_id'];
            //     }
            // }
            $str = $info['node_ids'];
            $arr_count = Db::name('rl_node')->where('type_id', 'in', $info['type'])->where('node_id', 'in', $str)->column('node_id');
            $str = implode(',', $arr_count);
            /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            $arr_count = explode(',', $str);
            $count = Db::name('rl_hise_service_pro_process')->where('gc_azdid', $azdid)->column('azjd');
            $node_diff = array_diff($arr_count, $count);


            if (empty($node_diff)) {
                $data_up = ['gczt' => $this->status['调试'], 'update_time' => time()];

            }

            if (!empty($data_up)) {
                RlHIseServiceProjectModel::where('azd_id', $azdid)->update($data_up);
                $this->updataInspect($azdid, $data_up['gczt']);
            }
            //删除多余的照片
            $removeimage = [];
            $removeimage = $this->request->param("removeimage/a");
            foreach ($removeimage as $keyi => $valuei) {
               // unlink(ROOT_PATH . 'public' . $valuei);
            }
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    //查看
    public function lookNode()
    {
        $type_id = $this->request->param('type_id', '');
        $node_id = $this->request->param('node_id', '');
        $azdid = $this->request->param('azd_id', '');
        if (!$type_id || !$node_id || !$azdid) $this->error('参数错误');
        $data['cplp'] = $type_id;
        $data['azjd'] = $node_id;
        $data['gc_azdid'] = $azdid;
        $res = Db::name('rl_hise_service_pro_process')->where($data)->find();
        $row = Db::name('rl_inspect')->where('azd_id', $azdid)->where('is_problem', 1)->where('inspect_status', 2)->field('id,uid,u_name,is_problem,conclusion')->select();

        $whre['azd_id'] = $azdid;
        $whre['node_id'] = $node_id;
        $inst_detail = Db::name('rl_hise_service_pro_inst_detail')->where($whre)->select()->toArray();
        $rows = [];
        if (!empty($row)) {
            $row_str = '';
            $row_arr = [];
            foreach ($row as $k => $v) {
                if ($k == 0) {
                    $row_str = $row_str . $v['id'];
                } else {
                    $row_str = $row_str . ',' . $v['id'];
                }
                $job_info = RlZsmsUserQueryModel::field('gw,bm_code')->where('zsms_user_query_id', $v['uid'])->where('ignore', 0)->find();

                if ($job_info['gw'] == '0' || $job_info['gw'] == '1' || $job_info['gw'] == '2') {
                    $compay_name = RlEtCustomerModel::where('jxsbm', $job_info['bm_code'])->field('jxs_mc')->find()['jxs_mc'];
                } else {
                    $compay_name = RlCompanyModel::where('orgdept', $job_info['bm_code'])->field('orgname')->find()['orgname'];

                }
                $row_arr[$v['id']] = $compay_name;
            }
//             $row_str = implode(',',$row);
            $where['tid'] = ['in', $row_str];
            $where['type'] = $type_id;
            $where['node'] = $node_id;
            $where['is_problem'] = 1;
            $rows = Db::name('rl_inspect_step')->where($where)->select()->toArray();

            foreach ($rows as $key => $val) {
                if ($val['no_specs_pic'] || $val['no_specs_pic_two'] || $val['no_specs_pic_three']) {
                    $rows[$key]['company'] = $row_arr[$val['tid']];
                    $rows[$key]['specs_pic'] = '';
                    $rows[$key]['specs_pic_two'] = '';
                    $rows[$key]['specs_pic_three'] = '';
                    foreach ($row as $k => $v) {
                        if ($v['id'] == $val['tid'])
                            $rows[$key]['u_name'] = $v['u_name'];
                        //查询到的都是有问题的
                        $rows[$key]['is_problem'] = '未验证';
                        $rows[$key]['conclusion'] = $v['conclusion'];
                    }
                } else {
                    unset($rows[$key]);
                }
            }

        }
        $node_data = Db::name('rl_node_data')->where('pid', $node_id)->select()->toArray();
        $this->success('请求成功', '', ['inspect' => $rows, 'process' => $res, 'inst_detail' => $inst_detail, 'node_data' => $node_data]);
    }

    //交付
    public function deliver()
    {
        $azd_id = $this->request->param('azd_id');
        $data = [
            'ktxtyxzc' => $this->request->param('ktxtyxzc', 0),
            'swjzjzc' => $this->request->param('swjzjzc', 0),
            'yzktsypx' => $this->request->param('yzktsypx', 0),
            'whbyzysx' => $this->request->param('whbyzysx', 0),
            'cjgzclpx' => $this->request->param('cjgzclpx', 0),
            'snjzyzc' => $this->request->param('snjzyzc', 0),
        ];
        foreach ($data as $key => $value) {
            if ($value != 1) {
                $this->error('以上内容有未完成项，请完成后再交付！');
            }
        }
        if (!$azd_id) $this->error('参数错误');
        $info = RlHIseServiceProjectModel::Where('azd_id', $azd_id)->field('jxs,type,ycgcsfjsbs,node_ids')->find();
        /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/

        // $company = RlEtCompanyModel::Where('jxsbm', $info['jxs'])->field('fgs')->find();
        // $where = $info['jxs'].','.$company['fgs'].'.'.$info['jxs'].','.'1';
        // $list = RlBmNodeRelModel::where('bm','in',$where)->field('node_id')->where('type_id','in',$info['type'])->select()->toArray();
        // $str = '';

        // foreach ($list as $k =>$v){
        //     if($k==0){
        //         $str = $str.$v['node_id'];
        //     }else{
        //         $str = $str.','.$v['node_id'];
        //     }
        // }
        $str = $info['node_ids'];
        $arr_count = Db::name('rl_node')->where('type_id', 'in', $info['type'])->where('node_id', 'in', $str)->column('node_id');
        // $arr_count = explode(',',$str);

        /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
        $count = Db::name('rl_hise_service_pro_process')->where('gc_azdid', $azd_id)->column('azjd');
        //比对节点
        // $node_diff = array_diff($arr_count,$count);
        // *********************************************
        $node_diff = $arr_count;
        foreach ($node_diff as $key => $value) {
            if (in_array($value, $count)) {
                unset($node_diff[$key]);
            }
        }
        //**********************************************
        if (!empty($node_diff)) {
            foreach ($node_diff as $v) {
                if ($v) {
                    $node_diff_id = $v;
                    $node_info = Db::name('rl_node')->where('node_id', $node_diff_id)->field('type_id,node_name,is_hide')->find();
                    //如果是后加的隐蔽节点，交付时不验证是否上传照片
                    if ($node_info['is_hide']) {
                        if ($info['ycgcsfjsbs']) {

                        } else {
                            $type_name = Db::name('rl_type')->where('type_id', $node_info['type_id'])->field('type_name')->find()['type_name'];
                            $this->error('禁止交付，存在' . $type_name . '类型,' . $node_info['node_name'] . '节点未上传');
                        }
                    } else {

                        $type_name = Db::name('rl_type')->where('type_id', $node_info['type_id'])->field('type_name')->find()['type_name'];
                        $this->error('禁止交付，存在' . $type_name . '类型,' . $node_info['node_name'] . '节点未上传');
                    }
                }
            }

        }
        $txm = array_unique(Db::table('cmf_rl_hise_service_pro_inst_detail')->where('azd_id', $azd_id)->column('node_id'));
        $txm_count = Db::name('rl_node')->where('type_id', 'in', $info['type'])->where('node_id', 'in', $str)->where('is_up_txm', 1)->column('node_id');
        if (!$txm) $this->error('禁止交付，未扫码');
        $txm_node_diff = array_diff($txm_count, $txm);
        if (!empty($txm_node_diff)) {
            foreach ($txm_node_diff as $v) {
                $node_txm_diff_id = $v;

                $txm_node_info = Db::name('rl_node')->where('node_id', $node_txm_diff_id)->field('type_id,node_name,in_and_ex')->find();
                if ($txm_node_info['in_and_ex'] != 1) {

                    $txm_type_name = Db::name('rl_type')->where('type_id', $txm_node_info['type_id'])->field('type_name')->find()['type_name'];
                    $this->error('禁止交付，存在' . $txm_type_name . '类型' . $txm_node_info['node_name'] . '节点条形码未上传');
                }
            }
        }

        $res = Db::name('rl_inspect')->field('is_problem,is_model,inspect_status')->where('azd_id', $azd_id)->where('is_problem', 1)->find();
        if ($res) {
            if ($res['is_problem'] == 1) $this->error('禁止交付，巡检问题未验证');
        }
        $item = RlHiseServiceProjectModel::Where('azd_id', $azd_id)->find();
        if ($item['gczt'] == 5) {
            $this->success('已经交付成功，不能再次交付');
        }

        //------
        // $info = RlHIseServiceProjectModel::Where('azd_id',$azdid)->find();
        //-------
        if (!$item['ycgcwcsj'] || !$item['ycgcsfjsbs']) {
            $data_up = ['ycgcsfjsbs' => 1, 'ycgcwcsj' => time()];
            RlHIseServiceProjectModel::where('azd_id', $azd_id)->update($data_up);
        }
        //-----


        $data['gczt'] = $this->status['运行'];
        $data['update_time'] = time();
        $data['bx_kssj'] = time();
        $data['tssj'] = time();
        $data['wgsj'] = time();
        $data['tjrq'] = time();
//        $path_dir = '/pdf/'.date('Yhd');
//        $path = $path_dir.'/'.time().'.pdf';
//        if (!file_exists('..'.$path_dir)){
//              mkdir ('..'.$path_dir,0777,true);
//        }
//        $data['wgbgnr'] =$path;
        //发送内外机数据到门店
        $sendYuekeData=RlHIseServiceProjectModel::where('azd_id',$azd_id)->find();
        $nodeData=Db::table('cmf_rl_hise_service_pro_process')->where(['gc_azdid'=>$azd_id])->select();
        if($sendYuekeData['bbdh']!=''){
            $dataaz['USER_NAME']=$sendYuekeData['yh_name'];
            $dataaz['USER_PHONE']=$sendYuekeData['sj_one'];
            $dataaz['PROVICE_SAP']=$sendYuekeData['provice'];
            $dataaz['CITY_SAP']=$sendYuekeData['city'];
            $dataaz['QX_SAP']=$sendYuekeData['qx'];
            $dataaz['ADDRESS']=$sendYuekeData['address'];
            $dataaz['CTIME']=date('Y-m-d H:i:s');
            $dataaz['AZ_ON']=$azd_id;
            $dataaz['OUTDOOR_NUM']=0;
            $dataaz['INDOOR_NUM']=0;
            //外机数据
            $dataaz['OUTDOOR_SAP']=[];
            //内机数据
            $dataaz['INDOOR_SAP']=[];
            $dataaz['RECORD_NO']=$sendYuekeData['bbdh'];
            $dataaz['STATUS']=7;
            $dataaz['BRAND']=2;
            $dataaz['SERVER_CODE']=$sendYuekeData['azs'];
            $dataaz['SERVER_TEAM']=$sendYuekeData['azd'];
            $dataaz['SERVER_CAPTAIN']='';
            $dataaz['SERVER_MOBILE']='';
            //节点照片
            $dataaz['NODE']=[];
            if($sendYuekeData['azd']){
                $dataaz['SERVER_CAPTAIN']=Db::table('cmf_rl_et_azd')->where([
                    'engineer_code'=>$sendYuekeData['azd'],
                    'active_flag'=>1
                ])->value('engineer_name');
                $dataaz['SERVER_MOBILE']=Db::table('cmf_rl_et_azd')->where([
                    'engineer_code'=>$sendYuekeData['azd'],
                    'active_flag'=>1
                ])->value('phone');
            }
            //内wai机数据拼装
            $tiaomaData=Db::table('cmf_rl_hise_service_pro_inst_detail')->where([
                'azd_id'=>$azd_id
            ])->select();

            foreach($tiaomaData as $kn=>$vn){
                $xlhData=Db::table('cmf_rl_node')->where([
                    'node_id'=>$vn['node_id']
                ])->value('in_and_ex');
                if($xlhData==1){
                    $dataaz['INDOOR_SAP'][]=[
                        'CPJX'=>$vn['xhdm'],
                        'JQXLH'=>$vn['jqxlh'],
                        'HT_ON'=>$vn['chht']
                    ];
                }
                if($xlhData==2){
                    $dataaz['OUTDOOR_SAP'][]=[
                        'CPJX'=>$vn['xhdm'],
                        'JQXLH'=>$vn['jqxlh'],
                        'HT_ON'=>$vn['chht']
                    ];
                }
            }
            $dataaz['INDOOR_NUM']=count($dataaz['INDOOR_SAP']);
            $dataaz['OUTDOOR_NUM']=count($dataaz['OUTDOOR_SAP']);
            //节点数据
            if($nodeData){
                foreach($nodeData as $knode=>$vnode){
                    $arr['azjd_name']=Db::table('cmf_rl_node')->where(['node_id'=>$vnode['azjd']])->value('node_name');
                    $arr['product_type']=$vnode['product_type'];
                    $arr['azjd']=$vnode['azjd'];
                    $arr['img']=[];
                    if($vnode['tp_one']){
                        $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_one'];
                    }
                    if($vnode['tp_two']){
                        $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_two'];
                    }
                    if($vnode['tp_there']){
                        $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_there'];
                    }
                    if($vnode['tp_four']){
                        $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_four'];
                    }
                    if($vnode['tp_five']){
                        $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_five'];
                    }
                    $dataaz['NODE'][]=$arr;
                }
            }
$outdatas=$dataaz['OUTDOOR_SAP'];
            $dataaz['NODE']=json_encode( $dataaz['NODE']);
            $dataaz['INDOOR_SAP']=json_encode( $dataaz['INDOOR_SAP']);
            $dataaz['OUTDOOR_SAP']=json_encode( $dataaz['OUTDOOR_SAP']);
            if($outdatas){
		$Yueke=new YuekeController();
            $Yueke->PushAzdNeiWaiData($dataaz);
		}
         //   $Yueke=new YuekeController();
           // $Yueke->PushAzdNeiWaiData($dataaz);
        }
        //结束
        $row = RlHiseServiceProjectModel::Where('azd_id', $azd_id)->update($data);
        $this->updataInspect($azd_id, $data['gczt']);

        if ($row) {
//            $this->reportPdf($azd_id,$path);
            $this->success('交付成功');
        }
        $this->error('交付失败');

    }

    //生成报告书pdf
    public function reportPdf($azd_id = '', $path = '')
    {

//        $pdf = New Pdf("/usr/bin/wkhtmltopdf");
//
//        $service_project_id = RlHiseServiceProjectModel::where('azd_id',$azd_id)->field('hise_service_project_id')->find()['hise_service_project_id'];
        $path = '..' . $path;
//        $pdf->generate('http://rixcx.akng.net/pdf/pdf/pdf?service_project_id='.$service_project_id,$path);
//        return 1;
//        $path ='../'.'pdf/'.date('Yhd').'/'.date('his').rand(1,9).'.pdf';
//        $azd_id ='JZXCX201912191101397796';
        $service_project_id = RlHiseServiceProjectModel::where('azd_id', $azd_id)->field('hise_service_project_id')->find()['hise_service_project_id'];

        $data = $this->PrepositionReport($service_project_id);
        $name = $data['info']['yh_name'];
        $xb = $data['info']['xb'] == 1 ? '男' : '女';
        $address_detail = $data['info']['address_detail'];
        $address = $data['info']['address'];
        $phone = $data['info']['sj_one'];
        $wagshj = $data['info']['wgsj'];
        $config = [
            'mode' => 'utf-8'];
        $mpdf = new Mpdf($config);
        $mpdf->autoScriptToLang = true;
        $mpdf->autoLangToFont = true;
        $html = "<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf-8' />

		<meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no'/>
        <link rel='stylesheet' href='css/reset.css' />
        <link rel='stylesheet' href='css/main.css' />
	</head>
	<body>
		<div class='container'>
        <div class='water_top'> </div>
			<div class='install_top'>工程报告书</div>
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop.png' class='img' />业主信息
				</div>
				<div class='form'>
					<div class='bgwhite' style='padding-bottom: 20px;'>
						<div class='group'>
							<div class='box'>
								<label class='label'>姓名</label>
								<div class='mainform'>
									<div class='input'>$name</div>
								</div>
							</div>
						</div>
						<div class='group'>
							<div class='box'>
								<label class='label'>性别</label>
								<div class='mainform'>
									<div class='input'>$xb</div>
								</div>
							</div>
						</div>
						<div class='group'>
							<div class='box'>
								<label class='label'>手机</label>
								<div class='mainform'>
									<div class='input'>$phone</div>
								</div>
							</div>
						</div>
						<div class='group'>
							<div class='box'>
								<label class='label'>所在地区</label>
								<div class='mainform'>
									<div class='input'>$address</div>
								</div>
							</div>
						</div>
						<div class='group address_detail'>
							<div class='box'>
								<label class='label'>详细地址</label>
								<div class='mainform moreh'>
									<div class='input'>$address_detail</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop3.png' class='img' />安装记录
				</div>
				<div class='recode'>
					<div class='bgwhite'>";


        foreach ($data['node_list'] as $k => $v) {

            $html .= "<div class='re_li'><div class='titleo clearfix'><text class='txt'>";
            $html .= $v['node_name'];
            $html .= "</text><div class='time'><img src='images/icon-time-red.png' class='img' />";
            $html .= date("Y-h-d ", $v['jdqrsj']);
            $html .= "</div></div><div class='imglist clearfix'>";
            $html .= "<div class='ili'><img src=";
            if ($v['tp_one']) {
                $html .= "'.." . $v['tp_one'] . "'";
            } else {
                $html .= '';
            }
            $html .= " class='img' /></div>";
            $html .= "<div class='ili'><img src=";
            if ($v['tp_two']) {
                $html .= "'.." . $v['tp_two'] . "'";
            } else {
                $html .= '';
            }
            $html .= " class='img' /></div>";
            $html .= "<div class='ili'><img src=";
            if ($v['tp_there']) {
                $html .= "'.." . $v['tp_there'] . "'";
            } else {
                $html .= '';
            }
            $html .= " class='img' /></div>";
            $html .= "</div></div>";
        }


        $html .= "</div>
				</div>
			</div>
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop4.png' mode='aspectFit' class='img' />调试结论
				</div>
				<div class='bgwhite'>
					<div class='bugs'>
						<img src='images/icon-selected.png' mode='aspectFit' class='img' />调试合格
					</div>
				</div>
			</div>
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop6.png' mode='aspectFit' class='img'>
					用户须知
				</div>
				<div class='bgwhite'>
					<div class='instructions'>
						<div class='txt'>在您使用空调过程中需要注意以下几点事项：</div>
						<div class='op'>
							<div class='p'>
								<img src='images/icon-intro1.png' mode='aspectFit' class='img' />
								<div class='lo'>
									<span>待机耗电：</span>因为空调在不运转的情况下存在待机耗电，如果长时间不用空调，可切断空调电源；
								</div>
							</div>
							<div class='p'>
								<img src='images/icon-intro2.png' mode='aspectFit' class='img' />
								<div class='lo'>
									<span>预热保护：</span>长时间断电后，需要使用空调时，请提前一天通电；
								</div>
							</div>
							<div class='p'>
								<img src='images/icon-intro3.png' mode='aspectFit' class='img' />
								<div class='lo'>
									<span>清洗保养：</span>空调使用时间较长时，会导致灰尘堆积到过滤网上，影响效果和增大噪音，建议定期清洗过滤网。
								</div>
							</div>
						</div>
						<div class='time'>$wagshj</div>
					</div>
					<div class='hot_phone'><img src='images/icon-phone2.png' class='icon' />安装服务监督热线：400-860-1111</div>
				</div>
			</div>

		</div>
	</body>
</html>";

        $mpdf->WriteHTML($html);
        $mpdf->Output($path, "f");
    }

    //节点列表前置
    public function nodePreposition()
    {
        $azd_id = $this->request->param('azd_id', '');
        $type_id = $this->request->param('type_id', '');
        if (!$azd_id || !$type_id) $this->error('参数错误');
        $info = RlHIseServiceProjectModel::Where('azd_id', $azd_id)->field('jxs')->find();
        $company = RlEtCompanyModel::Where('jxsbm', $info['jxs'])->field('fgs')->find();
        $where = $info['jxs'] . ',' . $company['fgs'] . '.' . $info['jxs'] . ',' . '1';
        $list = RlBmNodeRelModel::where('bm', 'in', $where)->field('node_id')->where('type_id', $type_id)->select();
        $str = '';
        foreach ($list as $v) {
            if (strlen($str)) {
                $str = $str . ',' . $v['node_id'];
            } else {
                $str = $v['node_id'];
            }
        }
        return $str;
    }

    //扫描条形码
    public function scanning()
    {
        $xlh = $this->request->param('jqxlh', '');
        $azd_id = $this->request->param('azd_id', '');
        $node_id = $this->request->param('node_id', '');
        if (!$xlh || !$azd_id || !$node_id) $this->error('参数错误');
        $xlh_len = strlen($xlh);
        if ($xlh_len != 12 && $xlh_len != 14 && $xlh_len != 23) $this->error('条形码解析错误,请重新扫码');
        $status = RlHiseServiceProjectModel::where('azd_id', $azd_id)->field('gczt')->find()['gczt'];
        if ($status == 0) $this->error('未进场不能扫描条形码');
        $azd_id_arr = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('azd_id', $azd_id)->where('status', '<>', 3)->where('jqxlh', $xlh)->column('azd_id');
        if (!empty($azd_id_arr)) {
            if (in_array($azd_id, $azd_id_arr)) {
                $this->error('该机器序列号已添加');
            }
        }
        $res = Db::table('cmf_rl_zsms_pro_query')->where('jqxl', $xlh)->where('active_flag', 1)->find();
        $url = 'http://172.16.43.237:8080/powerwms6service/webservice/commonservice.asmx?op=GetShippedSNForCRM';
        $uid = '00000000005';
        $pwd = 'crm';
        $data['uid'] = $uid;
        $data['pwd'] = $pwd;
        $data['serialno'] = $xlh;
        try {
            // libxml_disable_entity_loader(false);
            // if(config('is_ceshi'))
            if (false) {
                $client = new \SoapClient('http://172.16.120.93/TESTSERVICE/WebService/CommonService.asmx?WSDL');//测试
            } else {
                $client = new \SoapClient('http://172.16.43.237:8080/powerwms6service/webservice/commonservice.asmx?WSDL');
            }

            $xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <soapenv:Body>
             <GetShippedSNForCRM xmlns="CommonService">
                 <pwd>crm</pwd>
                  <uid>00000000005</uid>
                  <serialno>';
            $xml .= $xlh;
            $xml .= '</serialno>
            </GetShippedSNForCRM>
            </soapenv:Body>
            </soapenv:Envelope>';
            $return = $client->__doRequest($xml, 'http://172.16.43.237:8080/powerwms6service/webservice/commonservice.asmx?WSDL', 'getAlogpsResults', 1);
            // $return = $client->__doRequest($xml,'http://172.16.120.93/TESTSERVICE/WebService/CommonService.asmx?WSDL','getAlogpsResults',1);//测试
            // print_r($return);

            $result = substr($return, stripos($return, "{"));
            $result = substr($result, 0, stripos($result, "}") + 1);
            $curl = json_decode($result, true);

        } catch (SOAPFault $e) {
            // print_r('Exception:'.$e);
            $curl = [];
        }
        if ($status < 3) {
            $map['status'] = 1;
        } else {
            $map['status'] = 2;
        }
        $map['azd_id'] = $azd_id;
        $map['jqxlh'] = $xlh;
        $map['sjfhsj'] = '';
        $map['chht'] = '';
        $map['chjxs'] = '';
        $map['smsj'] = time();
        $map['sfcf'] = 0;
        $map['node_id'] = $node_id;
        $map['jxcg_bs'] = 'E';
        $map['cfazdh'] = '';
        $map['create_time'] = time();
        if (!empty($curl)) {
            $matnr = Db::name('rl_matnr')->where('xh_code', $curl['sku_id'])->find();
            $rl_node = Db::name('rl_node')->where('node_id', $node_id)->find();
            if ($matnr['nwj_flag'] != 1 && $matnr['nwj_flag'] != 2) {
                $this->error('条码对应机型未维护内外机标识');
            }
            //内机条码必须在内机节点上传
            if ($rl_node['in_and_ex'] == 1) {
                if ($matnr['nwj_flag'] != 1) {
                    $this->error('录入条码不是内机条码，请到外机节点上传');
                }
            }
            if ($rl_node['in_and_ex'] != 1) {
                if ($matnr['nwj_flag'] != 2) {
                    $this->error('录入条码不是外机条码，请到内机节点上传');
                }
            }
            $map['sjfhsj'] = substr($curl['actual_ship_date'], 0, 10);
            $map['chht'] = $curl['contractno'];
            $map['chjxs'] = $curl['customername'];
            $map['smsj'] = time();
            $map['jxcg_bs'] = 'S';
            $map['xhdm'] = $curl['sku_id'];
            $results = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('status', '<>', 3)->where('jqxlh', $xlh)->find()['azd_id'];
            if ($results) {
                $map['sfcf'] = 1;
                $map['cfazdh'] = $results;
                return json(['msg' => '该条形码已在其他安装单存在,请上传图片', 'code' => 10010, 'data' => $map]);
            }
            if ($matnr['brand'] != 2) $this->error('录入条码品牌与安装单品牌不一致，请重新扫码');
            // if($status==$this->status['隐蔽施工已结束']||$status==$this->status['调试']){
            //     $matnr =  Db::name('rl_matnr')->where('xh_code',$curl['sku_id'])->find();
            //     if($matnr['nwj_flag']==1) $this->error('隐蔽工程已结束不能再扫描条形码');
            //     // $map['status']=  2;
            // }
        }
        if ($res) {
            $map['sfcf'] = 1;
            $map['cfazdh'] = $res['azd'];
//            $map['jxcg_bs'] =  'E';
        }
        $count = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('azd_id', $azd_id)->count();
        $map['xh'] = $count + 1;
        $map['update_time'] = time();

        if (!empty($azd_id_arr)) {
            if (!in_array($azd_id, $azd_id_arr)) {
                $map['sfcf'] = 1;
                $map['cfazdh'] = $azd_id_arr['azd_id'];
                return json(['msg' => '该机器序列号已添加且安装单号不一致,请上传图片', 'code' => 10010, 'data' => $map]);
            }

        }
        if (empty($curl)) {
            return json(['msg' => '上传条码不在发货记录中,请上传图片', 'code' => 10010, 'data' => $map]);
        }
        if ($res) {
            $map['sfcf'] = 1;
            $map['cfazdh'] = $res['azd'];
            return json(['msg' => '该条形码已在其他安装单存在,请上传图片', 'code' => 10010, 'data' => $map]);
        }
        $result = Db::table('cmf_rl_hise_service_pro_inst_detail')->insert($map);
        if ($result) $this->success('添加成功');
    }

    //删除条码信息
    public function dltscanning()
    {
        $data = $this->request->param();
        $pro_inst_detail = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('hise_service_pro_inst_detail_id', $data['hise_service_pro_inst_detail_id'])->find();
        if ($pro_inst_detail) {
            $status = RlHiseServiceProjectModel::where('azd_id', $pro_inst_detail['azd_id'])->field('gczt')->find()['gczt'];
            // if($status<3)
            // {
            // $result = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('hise_service_pro_inst_detail_id',$data['hise_service_pro_inst_detail_id'])->delete();
            // }else{
            $result = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('hise_service_pro_inst_detail_id', $data['hise_service_pro_inst_detail_id'])->update(['status' => 3, 'update_time' => time()]);
            // }
            if ($result) {
                // if($pro_inst_detail['zpdz'])
                // {
                // unlink(ROOT_PATH.'public'.$pro_inst_detail['zpdz']);
                // }
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('没有找到对应条码信息');
        }
    }

    //条形码已存在上传图片
    public function scanningImage()
    {
        $data = $this->request->param();
        $imgUpload = new FunctionService;
        $image = $imgUpload->upload();
        if (!$data['jqxlh'] || !$image || !$data['azd_id']) $this->error('参数错误');
        if (!isset($image['img'])) {
            $this->error('文件错误');
        }
        $data['zpdz'] = $image['img'];
        $data['update_time'] = time();
        $count = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('azd_id', $data['azd_id'])->count();
        $data['xh'] = $count + 1;
        $res = Db::table('cmf_rl_hise_service_pro_inst_detail')->insert($data);
        if ($res) $this->success('保存成功');
        $this->error('保存失败');
    }

    //报告书
    public function report()
    {
        $service_project_id = $this->request->param('service_project_id', '');
        if (!$service_project_id) $this->error('参数错误');
        $data = $this->PrepositionReport($service_project_id);
        $this->success('请求成功', '', $data);
    }

    //报告书前置
    public function PrepositionReport($service_project_id)
    {
        //多表引入查询方法
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->field('azd_id,yh_name,xb,sj_one,provice,city,qx,address,qyfgs,jxs
        ,azs,azd,fgsfwfzr,cplb,yyjc_sj,jkh,zjbxq,zybjbxq,jxsxsy,bz,pic_one,pic_two,wgsj')->find();
        $info['wgsj'] = date('Y-m-d', $info['wgsj']);
//        if(!$info) $this->error('该用户不存在');
        if (empty($info['qx'])) {
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $info['city'])->field('dq_name')->select()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code', $info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice . $city . $info['address'];
            $info['address'] = $provice . $city;
        } else {
            $xq = Db::name('rl_hise_qx_table')->where('qx_code', $info['qx'])->field('qx_name')->find()['qx_name'];
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code', $info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice . $city . $xq . $info['address'];
            $info['address'] = $provice . $city . $xq;
        }
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm', $info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $info['azs_name'] = RlEtAzsModel::where('server_code', $info['azs'])->field('server_name')->find()['server_name'];
        $info['azd_name'] = RlEtAzdModel::where('engineer_code', $info['azd'])->field('engineer_name')->find()['engineer_name'];
        $info['fgs_name'] = Db::name('rl_company')->where('orgdept', $info['qyfgs'])->field('orgname')->find()['orgname'];
        $info['yyjc_sj'] = date('Y-m-d', $info['yyjc_sj']);
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm', $info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $prefix = 'cmf_';
        $join = [
            ["{$prefix}rl_node node", 'node.node_id = pro.azjd'],
        ];
        $list = Db::name('rl_hise_service_pro_process')->alias('pro')
            ->join($join)
            ->where('pro.gc_azdid', $info['azd_id'])->order('node.sort desc')
            ->field('pro.jdqrsj,pro.cplp,pro.azjd,pro.tp_one,pro.tp_two,pro.tp_there,node.node_name')
            ->select();
        $data['info'] = $info;
        $data['node_list'] = $list;
        return $data;
    }

    public function statusCount()
    {

        // $jxs = $this->request->param('jxs');
        // if(!$jxs) $this->error('参数错误');
        $install_wan = $this->status['运行'];
        $install_dai = $this->dispatch['待安装队接受'];
        $where['gczt'] = $install_wan;
        // $where['jxs'] = $jxs;
        $where['azd'] = $this->user_info['engineer_code'];
        $data['已完成'] = RlHiseServiceProjectModel::Where($where)->count();
        $where['gczt'] = ['in', $this->status['未进场'] . ',' . $this->status['已进场'] . ',' . $this->status['隐蔽施工中'] . ',' . $this->status['隐蔽施工已结束'] . ',' . $this->status['调试']];
        $data['进行中'] = RlHiseServiceProjectModel::where($where)->where('zzdpdzt', $this->dispatch['安装队已接受'])->count();
        $where['zzdpdzt'] = $install_dai;
        unset($where['gczt']);
        $where['gczt'] = ['<>', 'X'];
        $data['待接收'] = RlHiseServiceProjectModel::Where($where)->count();
        $arr = [
            [
                'img' => '../../../images/install-icon1.png',
                'imged' => '../../../images/install-icon1-h1.png',
                'number' => $data['待接收'],
                'title' => '待接收',
            ],
            [
                'img' => '../../../images/install-icon2.png',
                'imged' => '../../../images/install-icon2-h.png',
                'number' => $data['进行中'],
                'title' => '进行中',
            ],
            [
                'img' => '../../../images/install-icon3.png',
                'imged' => '../../../images/install-icon3-h.png',
                'number' => $data['已完成'],
                'title' => '已完成',
            ],

        ];
        $this->success('请求成功', '', $arr);
    }

    //上传照片
    public function img(\think\Request $request)
    {
        $imgUpload = new FunctionService;
        // dump($request->param());die;
        $place = $request->param('place', '是空的，没有值');
        $uptype = $request->param('uptype', 0);
        $result = $imgUpload->upload();
        if ($result['code'] == 1 && $uptype == 0) {
            $logo = ROOT_PATH . 'logo.png';
            // 旋转图像
            $logo_rotate = \think\Image::open($logo)->rotate(-45)->save(ROOT_PATH . 'public/uploads/logo_45.png');
            $logo_45 = ROOT_PATH . 'public/uploads/logo_45.png';
            // 添加水印
            $this->water_all(ROOT_PATH . 'public' . $result['img'], $logo_45, ROOT_PATH . 'public' . $result['img'], 15);
            $image = \think\Image::open(ROOT_PATH . 'public' . $result['img']);
            //$image->water('./logo.png', \think\Image::WATER_NORTHWEST, 50);
            $image->text($place . "\n" . date('Y-m-d H:i:s'), ROOT_PATH . 'msyh.ttc', 14, '#FF0000', 9, 0, 0)->save(ROOT_PATH . 'public' . $result['img']);
        }
        return json($result);
    }

    /**
     * 添加全屏水印
     * @param  [type] $origin [原图]
     * @param [type] $[logo] [水印图片]
     * @param [type] $[pathname] [保存路径]
     * @param [type] $[opacity] [透明度]
     * @return [type]         [description]
     */
    public function water_all($origin, $logo, $pathname, $opacity = 20)
    {
        $im = imagecreatefromstring(file_get_contents($origin));
        //获取水印源
        $watermark = imagecreatefromstring(file_get_contents($logo));
        //获取图、水印 宽高类型
        list($bgWidth, $bgHight, $bgType) = getimagesize($origin);
        list($logoWidth, $logoHight, $logoType) = getimagesize($logo);
        //定义平铺数据
        $x_length = $bgWidth - 10; //x轴总长度
        $y_length = $bgHight - 10; //y轴总长度
        //创建透明画布 伪白色
        $w = imagesx($watermark);
        $h = imagesy($watermark);
        $cut = imagecreatetruecolor($w, $h);
        $white = imagecolorallocatealpha($cut, 255, 255, 255, 0);
        imagefill($cut, 0, 0, $white);
        //整合水印
        imagecopy($cut, $watermark, 0, 0, 0, 0, $w, $h);
        //循环平铺水印
        for ($x = 0; $x < $x_length; $x++) {
            for ($y = 0; $y < $y_length; $y++) {
                if ($x == 0 && $y == 0) {
                    imagecopymerge($im, $cut, $x, $y, 0, 0, $logoWidth, $logoHight, $opacity);
                }
                /*    if($x > $x_length - $logoWidth && $y > $y_length - $logoHight){

                        imagecopymerge($im, $cut, $x, $y, 0, 0, $logoWidth, $logoHight, $opacity);
                    }*/
                $y += $logoHight;
            }
            $x += $logoWidth;
        }
        imagecopymerge($im, $cut, $x_length - $logoWidth, $y_length - $logoHight, 0, 0, $logoWidth, $logoHight, $opacity);
        //header("Content-type:image/png");
        // imagejpeg 的第二个参数不传, 默认是显示图片
        imagejpeg($im, $pathname);
        empty($im) || imagedestroy($im);
    }

    public function apply()
    {
        $service_project_id = $this->request->param('service_project_id');
        if (!$service_project_id) $this->error('请求失败', '');
        $where['hise_service_project_id'] = $service_project_id;
        $row = RlHiseServiceProjectModel::where($where)->field('jxs,azd_id')->find()->toArray();
        $data['jxsmc'] = RlEtCustomerModel::where('jxsbm', $row['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $data['azd_id'] = $row['azd_id'];
        $data['jxsbm'] = $row['jxs'];
        $data['service_project_id'] = $service_project_id;
        $this->success('请求成功', $data);
    }

    public function applyUpload()
    {
        $azd_id = $this->request->param('azd_id');
        $id = $this->request->param('service_project_id');
        if (!$azd_id || !$id) $this->error('请求失败', '');
        // 不能重复提交
        $ishas = RlApplyDelayModel::where(['status' => 0, 'hise_service_project_id' => $azd_id, 'azd_id' => $azd_id])->find();
        if ($ishas) $this->error('请不要重复提交');
        $where['azd_id'] = $azd_id;
        $res = RlHiseServiceProjectModel::where($where)->field('ycgcsfjsbs')->find()['ycgcsfjsbs'];
        if (!$res) $this->error('未满足条件不能提交', '');
        $data['jxsmc'] = $this->request->param('jxsmc', '');
        $data['jxsbm'] = $this->request->param('jxsbm', '');
        $data['yqsqzq'] = $this->request->param('yqsqzq', '');
        $data['img'] = $this->request->param('img', '');
        $data['azd_id'] = $azd_id;
        $data['hise_service_project_id'] = $id;
        $data['status'] = 0;
        RlApplyDelayModel::create($data);
        RlHiseServiceProjectModel::where($where)->update(['is_apply_delay' => 1, 'update_time' => time()]);
        $this->success('申请成功', '');
    }

    public function updataInspect($azd_id, $status)
    {
        //向巡检单同步工程状态字段
//         Db::name('rl_inspect')->where('azd_id',$azd_id)->update(['progress'=>$status]);
    }

    public function userlist()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['keyword']) && $param['keyword']) {
            $where['engineer_name|phone'] = ['like', '%' . $param['keyword'] . '%'];
        }
        $userlist = Db::name('rl_et_azd')->where('server_code', $this->user_info['server_code'])->where($where)->select()->toArray();
        $server_name = Db::name('rl_et_azs')->where('server_code', $this->user_info['server_code'])->value('server_name');
        foreach ($userlist as $key => $value) {
            $userlist[$key]['server_name'] = $server_name;
        }
        $this->success('查询成功', '', ['userlist' => $userlist]);
    }

    //节点图片删除，更换
    // public function nodeimgupdate()
    // {
    //     $param = $this->request->param();
    //     $type = $param['type'];//类型，删除，新增，修改
    //     $newimg = $param['newimg'];//新图片地址
    //     $img = $param['img'];//旧图片地址
    //     $azjd = $param['azjd'];//节点id
    //     $gc_azdid = $param['gc_azdid'];//安装单编号
    //     $reslut = 0;
    //     if($type || $azjd || $gc_azdid)
    //     {
    //         $this->error('参数错误');
    //     }
    //     //获取项目节点数据
    //     $process = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->find();
    //     if($type==1)
    //     {
    //         //如果是删除
    //         if($process['tp_one'] == $img)
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_one'=>'']);
    //             unlink(ROOT_PATH.'public'.$process['tp_one']);
    //         }
    //         if($process['tp_two'] == $img)
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_two'=>'']);
    //             unlink(ROOT_PATH.'public'.$process['tp_two']);
    //         }
    //         if($process['tp_there'] == $img)
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_there'=>'']);
    //             unlink(ROOT_PATH.'public'.$process['tp_there']);
    //         }
    //     }elseif($type==2){
    //         //如果是修改
    //         if($process['tp_one'] == $img)
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_one'=>$newimg]);
    //             unlink(ROOT_PATH.'public'.$process['tp_one']);
    //         }
    //         if($process['tp_two'] == $img)
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_two'=>$newimg]);
    //             unlink(ROOT_PATH.'public'.$process['tp_two']);
    //         }
    //         if($process['tp_there'] == $img)
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_there'=>$newimg]);
    //             unlink(ROOT_PATH.'public'.$process['tp_there']);
    //         }
    //     }elseif($type==3){
    //         //如果是添加
    //         if(!$process['tp_one'])
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_one'=>$newimg]);
    //         }
    //         if(!$process['tp_two'])
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_two'=>$newimg]);
    //         }
    //         if(!$process['tp_there'])
    //         {
    //             $reslut = Db::name('rl_hise_service_pro_process')->where('gc_azdid',$gc_azdid)->where('azjd',$azjd)->update(['tp_there'=>$newimg]);
    //         }
    //     }
    //     if($reslut)
    //     {
    //         $this->success('操作成功');
    //     }else{
    //         $this->error('操作失败');
    //     }

    // }
    public function  updateZdyx(){
        // die;
        $azdid = $this->request->param('azd_id','');
        $type_id = $this->request->param('type_id','');
        $node_id = $this->request->param('node_id','');
        $node_name = $this->request->param('node_name','');
        $arrayFile = $this->request->param("image/a");// dump($arrayFile);die;
        $node_info = Db::name('rl_node')->where('node_id',$node_id)->field('is_hide,is_up_txm,in_and_ex')->find();
        $is_hide = $node_info['is_hide'];
        $is_up_txm = $node_info['is_up_txm'];
        $in_and_ex = $node_info['in_and_ex'];
        if($is_up_txm){
            $txm_is_up = Db::name('rl_hise_service_pro_inst_detail')->where('azd_id',$azdid)->where('node_id',$node_id)->find();
            if($in_and_ex==2)
            {
                if(empty($txm_is_up)) $this->error('请先上传该节点条形码！');
            }
        }
        $product_type = Db::name('rl_type')->where('type_id',$type_id)->field('product_type')->find()['product_type'];
        $reszdyxsj = RlHiseServiceProjectModel::where('azd_id',$azdid)->field('zdyxbs')->find()->toArray();
        //dump($reszdyxsj->toArray());die;
        if($reszdyxsj['zdyxbs']==1){
            // $data_zdyxbs['update_time']=time();
            // $data_zdyxbs['zdyxbs']=0;
            // $data_zdyxbs['is_zdyx']=1;
            // $data=RlHIseServiceProjectModel::where('azd_id',$azdid)->update($data_zdyxbs);
            $data['cplp'] = $type_id;
            $data['azjd'] = $node_id;
            $data['azjd_name'] = $node_name;
            $data['gc_azdid'] = $azdid;
            $data['jdqrsj'] = time();
            $data['update_time'] = time();
            $data['product_type'] = $product_type;
            if(empty($arrayFile)) $this->error('图片为空 不能保存');
            foreach ($arrayFile as $k => $File){

                if(!$File) $this->error('图片为空，不能保存');
                if($k==0)  $data['tp_one'] = $File;
                if($k==1)  $data['tp_two'] = $File;
                if($k==2)  $data['tp_there'] = $File;
                if($k==3)  $data['tp_four'] = $File;
                if($k==4)  $data['tp_five'] = $File;
            }
            $rl_hise_service_pro_process = Db::name('rl_hise_service_pro_process')->where('azjd',$node_id)->where('gc_azdid',$azdid)->find();
            if($rl_hise_service_pro_process)
            {
                $this->error('此节点已保存，不能重复保存');
            }
            $data_zdyxbs['update_time']=time();
            $data_zdyxbs['zdyxbs']=1;
            $data_zdyxbs['is_zdyx']=1;
            $datas=RlHIseServiceProjectModel::where('azd_id',$azdid)->update($data_zdyxbs);

            $res =  Db::name('rl_hise_service_pro_process')->insert($data);
            //发送内外机数据到门店
            $sendYuekeData=RlHIseServiceProjectModel::where('azd_id',$azdid)->find();
            $nodeData=Db::table('cmf_rl_hise_service_pro_process')->where(['gc_azdid'=>$azdid])->select();
            if($sendYuekeData['bbdh']!=''){
                $dataaz['USER_NAME']=$sendYuekeData['yh_name'];
                $dataaz['USER_PHONE']=$sendYuekeData['sj_one'];
                $dataaz['PROVICE_SAP']=$sendYuekeData['provice'];
                $dataaz['CITY_SAP']=$sendYuekeData['city'];
                $dataaz['QX_SAP']=$sendYuekeData['qx'];
                $dataaz['ADDRESS']=$sendYuekeData['address'];
                $dataaz['CTIME']=date('Y-m-d H:i:s');
                $dataaz['AZ_ON']=$azdid;
                $dataaz['OUTDOOR_NUM']=0;
                $dataaz['INDOOR_NUM']=0;
                //外机数据
                $dataaz['OUTDOOR_SAP']=[];
                //内机数据
                $dataaz['INDOOR_SAP']=[];
                $dataaz['RECORD_NO']=$sendYuekeData['bbdh'];
                $dataaz['STATUS']=9;
                $dataaz['BRAND']=2;
                $dataaz['SERVER_CODE']=$sendYuekeData['azs'];
                $dataaz['SERVER_TEAM']=$sendYuekeData['azd'];
                $dataaz['SERVER_CAPTAIN']='';
                $dataaz['SERVER_MOBILE']='';
                //节点照片
                $dataaz['NODE']=[];
                if($sendYuekeData['azd']){
                    $dataaz['SERVER_CAPTAIN']=Db::table('cmf_rl_et_azd')->where([
                        'engineer_code'=>$sendYuekeData['azd'],
                        'active_flag'=>1
                    ])->value('engineer_name');
                    $dataaz['SERVER_MOBILE']=Db::table('cmf_rl_et_azd')->where([
                        'engineer_code'=>$sendYuekeData['azd'],
                        'active_flag'=>1
                    ])->value('phone');
                }
                //内wai机数据拼装
                $tiaomaData=Db::table('cmf_rl_hise_service_pro_inst_detail')->where([
                    'azd_id'=>$azdid
                ])->select();

                foreach($tiaomaData as $kn=>$vn){
                    $xlhData=Db::table('cmf_rl_node')->where([
                        'node_id'=>$vn['node_id']
                    ])->value('in_and_ex');
                    if($xlhData==1){
                        $dataaz['INDOOR_SAP'][]=[
                            'CPJX'=>$vn['xhdm'],
                            'JQXLH'=>$vn['jqxlh'],
                            'HT_ON'=>$vn['chht']
                        ];
                    }
                    if($xlhData==2){
                        $dataaz['OUTDOOR_SAP'][]=[
                            'CPJX'=>$vn['xhdm'],
                            'JQXLH'=>$vn['jqxlh'],
                            'HT_ON'=>$vn['chht']
                        ];
                    }
                }
                $dataaz['INDOOR_NUM']=count($dataaz['INDOOR_SAP']);
                $dataaz['OUTDOOR_NUM']=count($dataaz['OUTDOOR_SAP']);
                //节点数据
                if($nodeData){
                    foreach($nodeData as $knode=>$vnode){
                        $arr['azjd_name']=Db::table('cmf_rl_node')->where(['node_id'=>$vnode['azjd']])->value('node_name');
                        $arr['product_type']=$vnode['product_type'];
                        $arr['azjd']=$vnode['azjd'];
                        $arr['img']=[];
                        if($vnode['tp_one']){
                            $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_one'];
                        }
                        if($vnode['tp_two']){
                            $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_two'];
                        }
                        if($vnode['tp_there']){
                            $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_there'];
                        }
                        if($vnode['tp_four']){
                            $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_four'];
                        }
                        if($vnode['tp_five']){
                            $arr['img'][]='https://rlwxaz.hisensehitachi.com'.$vnode['tp_five'];
                        }
                        $dataaz['NODE'][]=$arr;
                    }
                }
                $dataaz['NODE']=json_encode( $dataaz['NODE']);
                $dataaz['INDOOR_SAP']=json_encode( $dataaz['INDOOR_SAP']);
                $dataaz['OUTDOOR_SAP']=json_encode( $dataaz['OUTDOOR_SAP']);
                $Yueke=new YuekeController();
                $Yueke->PushAzdNeiWaiData($dataaz);
            }
            //结束
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
	 public function statusazsCount(){
        // $jxs = $this->request->param('jxs');
        // if(!$jxs) $this->error('参数错误');
        $install_wan = $this->status['运行'];
        $install_dai = $this->dispatch['待安装队接受'];
        $where['gczt'] = $install_wan;
        // $where['jxs'] = $jxs;
        $where['azs'] = $this->user_info['server_code'];
        $data['已完成'] = RlHiseServiceProjectModel::Where($where)->count();
        $where['gczt'] =  ['in',$this->status['未进场'].','.$this->status['已进场'].','.$this->status['隐蔽施工中'].','.$this->status['隐蔽施工已结束'].','.$this->status['调试']];
        $data['进行中'] =RlHiseServiceProjectModel::where($where)->where('zzdpdzt',$this->dispatch['安装队已接受'])->count();
        $where['zzdpdzt'] = $install_dai;
        unset($where['gczt']);
        $where['gczt'] = ['<>','X'];
        $data['待接收'] =RlHiseServiceProjectModel::Where($where)->count();
        $arr = [
            [
                'img'=> '../../../images/install-icon1.png',
                'imged'=> '../../../images/install-icon1-h1.png',
                'number'=>  $data['待接收'],
                'title'=> '待接收',
            ],
            [
                'img'=> '../../../images/install-icon2.png',
                'imged'=> '../../../images/install-icon2-h.png',
                'number'=>  $data['进行中'],
                'title'=> '进行中',
            ],
            [
                'img'=> '../../../images/install-icon3.png',
                'imged'=> '../../../images/install-icon3-h.png',
                'number'=>$data['已完成'],
                'title'=> '已完成',
            ],
        ];

        $this->success('请求成功','',$arr);
    }
}

